/**
 * Created by Administrator on 2017/5/1.
 */
!(function() {
    var shopVerify = {
        userId: $.fn.cookie('userId'),
        
        init:function(){
            this.getCity();
            this.submit();
            this.addImgF();
            this.addImgY();
            this.addImgZ();
            this.delImg();
        },
        getCity:function(){
            //选择省市县
            $("#ssx").cityPicker({
                title: "选择省市县"
            });
            var max = $('#count_max').text();
            //文本超出限制
            $('#textarea').on('input', function(){
                var text = $(this).val();
                var len = text.length;
                $('#count').text(len);
                if(len > max){
                    $(this).closest('.weui_cell').addClass('weui_cell_warn');
                    $.toptips('字数超出限制','info');
                }
                else{
                    $(this).closest('.weui_cell').removeClass('weui_cell_warn');
                }
            });
        },
        addImgZ:function(){
            var f2 = document.querySelector('#add_shopImga');
            f2.onchange = function (e) {
                lrz(this.files[0],{width:750}).then(function (rst) {
                        var params = {
                            type: 2,
                            file: rst.base64,
                            name: rst.origin.name,
                            fileLength: rst.base64.length
                        };
                        $.ajax({
                            type: "POST",
                            url: API_URL+IMG_UPLOAD,
                            data: params,
                            dataType: "json",
                            beforeSend: function (XMLHttpRequest) {
                                // showLoader();
                            },
                            success: function (data) {
                                if(data.ok==true){
                                    $('#imgs1').attr('style','background-image:url('+data.result.smallUrl+')');
                                    $('#imgs1').attr('data-id',data.result.id);
                                    $('#imgs1').show();
                                    if($('#imgs1').attr('style')){
                                        $('#filedZ').hide();
                                    }else{
                                        $('#filedZ').show();
                                    }
                                    $('#idImageId').val(data.result.id);
                                }
                            },
                            complete: function (XMLHttpRequest, textStatus) {
                                // hideLoader();
                            },
                            error:function (XMLHttpRequest, textStatus, errorThrown) {
                                $.toptips("上传失败",'info');
                            }
                        });
                        return rst;
                    })
                    .catch(function (err) {
                        $.toptips(err);
                    })
            }
        },
        addImgF:function(){
            var f2 = document.querySelector('#add_shopImgb');
            f2.onchange = function (e) {
                lrz(this.files[0],{width:750}).then(function (rst) {
                        var params = {
                            type: 2,
                            file: rst.base64,
                            name: rst.origin.name,
                            fileLength: rst.base64.length
                        };
                        $.ajax({
                            type: "POST",
                            url: API_URL+IMG_UPLOAD,
                            data: params,
                            dataType: "json",
                            beforeSend: function (XMLHttpRequest) {
                                // showLoader();
                            },
                            success: function (data) {
                                if(data.ok==true){
                                    $('#imgs2').attr('style','background-image:url('+data.result.smallUrl+')');
                                    $('#imgs2').attr('data-id',data.result.id);
                                    $('#imgs2').show();
                                    if($('#imgs2').attr('style')){
                                        $('#filedF').hide();
                                    }else{
                                        $('#filedF').show();
                                    }
                                    $('#idOtherImageId').val(data.result.id);
                                }
                            },
                            complete: function (XMLHttpRequest, textStatus) {
                                // hideLoader();
                            },
                            error:function (XMLHttpRequest, textStatus, errorThrown) {
                                $.toptips("上传失败",'info');
                            }
                        });
                        return rst;
                    })
                    .catch(function (err) {
                        $.toptips(err,'info');
                    })
            }
        },
        addImgY:function(){
            var f2 = document.querySelector('#add_shopImgc');
            f2.onchange = function (e) {
                lrz(this.files[0],{width:750}).then(function (rst) {
                        var params = {
                            type: 2,
                            file: rst.base64,
                            name: rst.origin.name,
                            fileLength: rst.base64.length
                        };
                        $.ajax({
                            type: "POST",
                            url: API_URL+IMG_UPLOAD,
                            data: params,
                            dataType: "json",
                            beforeSend: function (XMLHttpRequest) {
                                // showLoader();
                            },
                            success: function (data) {
                                if(data.ok==true){
                                    $('#imgs3').attr('style','background-image:url('+data.result.smallUrl+')');
                                    $('#imgs3').attr('data-id',data.result.id);
                                    $('#imgs3').show();
                                    if($('#imgs3').attr('style')){
                                        $('#filedY').hide();
                                    }else{
                                        $('#filedY').show();
                                    }
                                    $('#licenseImageId').val(data.result.id);
                                }
                            },
                            complete: function (XMLHttpRequest, textStatus) {
                                // hideLoader();
                            },
                            error:function (XMLHttpRequest, textStatus, errorThrown) {
                                $.toptips("上传失败",'info');
                            }
                        });
                        return rst;
                    })
                    .catch(function (err) {
                        $.toptips(err);
                    })
            }
        },
        delImg:function(){
            var img1 = $('#imgs1'),img2= $('#imgs2'),img3=$('#imgs3');
            img1.delegate('.icon-img-del','click',function(){
                $.confirm('您确定删除吗？','确认删除？',function(){
                    img1.removeAttr('style');
                    $('#idImageId').val('');
                    if(!img1.attr('style')){
                        $('#filedZ').show();
                    }
                    $(this).closest('li').remove();

                },function(){
                    $.toast('取消操作', 'cancel');
                })
            });
            img2.delegate('.icon-img-del','click',function(){
                $.confirm('您确定删除吗？','确认删除？',function(){
                    img2.removeAttr('style');
                    $('#idOtherImageId').val('');
                    if(!img2.attr('style')){
                        $('#filedF').show();
                    }
                    $(this).closest('li').remove();

                },function(){
                    $.toast('取消操作', 'cancel');
                })
            });
            img3.delegate('.icon-img-del','click',function(){
                $.confirm('您确定删除吗？','确认删除？',function(){
                    img3.removeAttr('style');
                    $('#licenseImageId').val('');
                    if(!img3.attr('style')){
                        $('#filedY').show();
                    }
                    $(this).closest('li').remove();

                },function(){
                    $.toast('取消操作', 'cancel');
                })
            });
        },
        verifySubmit:function(){
            var legalName = $('[name="legalName"]').val();
            var legalIdNumber = $('[name="legalIdNumber"]').val();
            var companyName = $('[name="companyName"]').val();
            var businessLicense= $('[name="businessLicense"]').val();
            var companyIdStr = $('[name="companyIdStr"]').val();
            var province = $('[name="province_city"]').val();
            var address = $('[name="address"]').val();
            var idImageId = $('[name="idImageId"]').val();
            var idOtherImageId = $('#idOtherImageId').val();
            var licenseImageId = $('#licenseImageId').val();
            if(legalName == ''){
                $.toptips("请输入法人代表姓名!",'info');
                return false;
            }
            if(legalIdNumber == ''){
                $.toptips("请输入法人代表身份证号码!",'info');
                return false;
            }
            if(companyName == ''){
                $.toptips("请输入企业名称!",'info');
                return false;
            }
            if(businessLicense == ''){
                $.toptips("请输入营业执照号",'info');
                return false;
            }
            if(companyIdStr == ''){
                $.toptips("请输入组织机构代码!",'info');
                return false;
            }
            if(province == ''){
                $.toptips("请输入营业执照所在省市!",'info');
                return false;
            }
            if(address == ''){
                $.toptips("请输入营业执照详细地址!",'info');
                return false;
            }
            if(idImageId == ''){
                $.toptips("请上传身份证反面照!",'info');
                return false;
            }
            if(idOtherImageId == ''){
                $.toptips("请上传身份证反面照!",'info');
                return false;
            }
            if(licenseImageId == ''){
                $.toptips("请上传营业执照!",'info');
                return false;
            }
            return true;
        },
        submit:function(){
            var self = this;
            var $form = $("#formShopVerify");
            $form.form();
           // if(self.verifySubmit){
                $("#formSubmitBtn").on('click',function(){
                    var legalName = $('[name="legalName"]').val();
                    var legalIdNumber = $('[name="legalIdNumber"]').val();
                    var companyName = $('[name="companyName"]').val();
                    var businessLicense= $('[name="businessLicense"]').val();
                    var companyIdStr = $('[name="companyIdStr"]').val();
                    var province = $('[name="province_city"]').val();
                    var address = $('[name="address"]').val();
                    var idImageId = $('#idImageId').val();
                    var idOtherImageId = $('#idOtherImageId').val();
                    var licenseImageId = $('#licenseImageId').val();
                    var param = {
                        userId:$.fn.cookie('userId'),
                        legalName:legalName,
                        legalIdNumber:legalIdNumber,
                        companyName:companyName,
                        businessLicense:businessLicense,
                        companyIdStr:companyIdStr,
                        province:province,
                        address:address,
                        idImageId:idImageId,
                        idOtherImageId:idOtherImageId,
                        licenseImageId:licenseImageId
                    };

                    $form.validate(function(error){
                        if(error){
                            $.toptips(error.msg);
                        }else{
                            $.ajax({
                                url:API_URL+SHOP_VERIFY_URL,
                                type:'post',
                                dataType:'jsonp',
                                data:param,
                                success:function(data){
                                    if(data.ok==true){
                                        $.alert("我们会尽快审核，谢谢您的配合!","",function(){
                                            window.location.href="set.html";
                                        });
                                    }else{
                                        $.toptips('资料不齐全!','info');
                                    }
                                },
                                error:function(data){
                                    $.toptips(data.msg);
                                }
                            })

                        }
                    });

                })
          //  }
        }
    };
    $(function(){
        shopVerify.init();
    })
})(Zepto);